﻿using Newtonsoft.Json;
using System;
using System.Text.Json.Serialization;

namespace ThresholdAssistant.DataContract.Result
{
    /// <summary>
    /// 兼容旧接口返回值
    /// </summary>
    [Serializable]
    public class NewJsonResult
    {
        /// <summary>
        /// 错误编码
        /// </summary>
        [JsonProperty("nResult"), JsonPropertyName("nResult")]
        public int Code { get; set; }

        /// <summary>
        /// 错误消息
        /// </summary>
        [JsonProperty("vcResult"), JsonPropertyName("vcResult")]
        public string Message { get; set; }

        /// <summary>
        /// 业务流水号
        /// </summary>
        [JsonProperty("vcSerialNo"), JsonPropertyName("vcSerialNo")]
        public string SerialNo { get; set; } = DateTime.Now.ToString("yyyyMMddHHmmssffff");

        /// <summary>
        /// 设置错误信息
        /// </summary>
        /// <param name="enumType">错误信息</param>
        /// <param name="serialNo">业务流水号</param>
        public NewJsonResult SetError<T>(T enumType, string serialNo = "") where T : Enum
        {
            Code = enumType.GetHashCode();
            Message = enumType.ToString();
            if (!string.IsNullOrWhiteSpace(serialNo))
            {
                SerialNo = serialNo;
            }
            return this;
        }

        /// <summary>
        /// 设置返回结果为成功
        /// </summary>
        /// <param name="serialNo">业务流水号</param>
        /// <returns></returns>
        public NewJsonResult SetSuccessful(string serialNo = null)
        {
            Code = 1;
            Message = "SUCCESS";
            if (!string.IsNullOrWhiteSpace(serialNo))
            {
                SerialNo = serialNo;
            }
            return this;
        }
    }

    /// <summary>
    /// 兼容旧接口返回值
    /// </summary>
    /// <typeparam name="T"></typeparam>
    [Serializable]
    public class NewJsonResult<T> : NewJsonResult
    {
        /// <summary>
        /// 数据内容
        /// </summary>
        [JsonProperty("Data"), JsonPropertyName("Data")]
        public T Data { get; set; }

        /// <summary>
        /// 设置错误信息
        /// </summary>
        /// <param name="enumType">错误信息</param>
        /// <param name="serialNo">业务流水号</param>
        public new NewJsonResult SetError<E>(E enumType, string serialNo = "") where E : Enum
        {
            Code = enumType.GetHashCode();
            Message = enumType.ToString();
            if (!string.IsNullOrWhiteSpace(serialNo))
            {
                SerialNo = serialNo;
            }
            return this;
        }

        /// <summary>
        /// 设置返回结果为成功
        /// </summary>
        /// <param name="serialNo"></param>
        /// <param name="data"></param>
        /// <returns></returns>
        public NewJsonResult SetSuccessful(string serialNo = null, T data = default)
        {
            Code = 1;
            Message = "SUCCESS";
            if (!string.IsNullOrWhiteSpace(serialNo))
            {
                SerialNo = serialNo;
            }
            return this;
        }
    }
}